<?php

namespace common\models;

use backend\controllers\BaseController;
use common\cache\DictCache;
use common\helpers\commonApi;
use common\helpers\TransformApi;
use common\helpers\UploadApi;
use common\helpers\ValidateApi;
use Yii;
use yii\db\Exception;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%running}}".
 *
 * @property int $id
 * @property int|null $userid 用户id
 * @property int|null $campus_id 校区id
 * @property string|null $name 名称
 * @property int|null $sex 性别
 * @property string|null $mobile 手机号
 * @property string|null $id_card_front 身份证正面
 * @property string|null $id_card_other 身份证反面
 * @property int|null $status 状态
 * @property int|null $review_status 审核状态
 * @property string|null $review_time 审核时间
 * @property string|null $review_remarks 审核备注
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 更新时间
 * @property float|null $can_brokerage 更新时间
 * @property float|null $total_brokerage 总佣金
 * @property float|null $already_brokerage 已提现佣金
 */
class Running extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%running}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['userid', 'campus_id', 'sex', 'status', 'review_status'], 'integer'],
            [['review_time', 'create_time', 'update_time'], 'safe'],
            [['name', 'mobile', 'id_card_front', 'id_card_other', 'review_remarks'], 'string', 'max' => 255],
            [['can_brokerage', 'total_brokerage', 'already_brokerage'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'userid' => '用户id',
            'campus_id' => '校区id',
            'name' => '名称',
            'sex' => '性别',
            'mobile' => '手机号',
            'id_card_front' => '身份证正面',
            'id_card_other' => '身份证反面',
            'status' => '状态',
            'review_status' => '审核状态',
            'review_time' => '审核时间',
            'review_remarks' => '审核备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'can_brokerage' => '可提现佣金',
            'total_brokerage' => '总佣金',
            'already_brokerage' => '已提现佣金',
        ];
    }

    /**
     * 入驻
     * @param $userid
     * @param $post
     * @return array
     */
    public function settled($userid, $post)
    {
        $result = $this->verific($post);
        if (!$result['success']) {
            return $result;
        }
        $model = new self();
        $model->load($result['data'], '');
        $model->review_status = 0;
        $model->userid = $userid;
        if ($model->save()) {
            return commonApi::message('申请成功，等待审核');
        }
        return commonApi::message(commonApi::getModelError($model), false);
    }

    /**
     * 验证
     * @param $data
     * @return array
     */
    public function verific($data)
    {
        if (empty($data['campus_id'])) {
            return commonApi::message('请选择校区', false);
        }
        $campus = Campus::getCampusAll(['id' => $data['campus_id'], 'status' => 1, 'delete' => 0], ['id', 'name']);
        if (!$campus) {
            return commonApi::message('该校区不存在', false);
        }
        if (empty($data['name'])) {
            return commonApi::message('请输入姓名', false);
        }
        if (empty($data['sex'])) {
            return commonApi::message('请输入性别', false);
        }
        $astrict_sex = DictCache::get('astrict_sex');
        $astrict_sex = array_keys($astrict_sex);
        if (!in_array($data['sex'], $astrict_sex)) {
            return commonApi::message('性别选择错误', false);
        }
        if (empty($data['mobile'])) {
            return commonApi::message('请输入手机号', false);
        }
        if (!ValidateApi::isMobilePhone($data['mobile'])) {
            return commonApi::message('手机号错误', false);
        }
        if (empty($data['id_card_front'])) {
            return commonApi::message('请上传身份证正面', false);
        }
        if (empty($data['id_card_other'])) {
            return commonApi::message('请上传身份证反面', false);
        }
        $upload = new UploadApi();
        $fileName = md5(time() . mt_rand(1000, 9999));
        $front = $upload->oss($data['id_card_front'], $fileName);
        if (!$front) {
            return commonApi::message('oss上传失败', false);
        }
        $fileName1 = md5(time() . mt_rand(1000, 9999));
        $other = $upload->oss($data['id_card_other'], $fileName1);
        if (!$other) {
            return commonApi::message('oss上传失败!!!', false);
        }
        $arr = [
            'campus_id' => $data['campus_id'],
            'name' => $data['name'],
            'sex' => $data['sex'],
            'mobile' => $data['mobile'],
            'id_card_front' => $front,
            'id_card_other' => $other,
        ];
        return commonApi::message('ok', true, $arr);
    }

    /**
     * 是不是跑腿
     * @param $userid 用户id
     * @return int[]
     */
    public function isRunn($userid)
    {
        $model = self::find()->where(['userid' => $userid])->one();
        $data = [
            'is_runn' => 0,
            'runn_status' => -1,
            'campus_id' => '',
            'runn_name' => '',
            'info' => [],
        ];
        if ($model) {
            $data['runn_status'] = $model->review_status;
            if ($model->review_status == 1 && $model->status == 1) {
                $data['is_runn'] = 1;
                $data['campus_id'] = $model->campus_id;
                $data['runn_name'] = $model->name;
                $data['info'] = $model->toArray();
            }
        }
        return $data;
    }

    /**
     * 操作佣金
     * @param $userid 用户id
     * @param $brokerage 佣金
     * @param string $orderSn 订单号
     * @return array
     */
    public function getBrokerage($userid, $brokerage, $orderSn)
    {
        $model = self::find()->where(['userid' => $userid])->one();
        if (!$model) {
            return commonApi::message('跑腿用户不存在', false);
        }
        $model->can_brokerage += $brokerage;
        $model->total_brokerage += $brokerage;
        TransformApi::tx(function () use ($model,$orderSn,$brokerage){
            if ($model->save()){
                Water::add($model->userid,1,1,$model->can_brokerage,$brokerage,"获得【{$orderSn}】跑腿佣金");
                return commonApi::message('操作成功');
            }
            throw new Exception(commonApi::getModelError($model));
        },$result);
    }
}
